package lzzy.edu.cn.demothymeleaf.controller;

import lzzy.edu.cn.demothymeleaf.entity.Article;
import lzzy.edu.cn.demothymeleaf.entity.Comment;
import lzzy.edu.cn.demothymeleaf.repository.ArticleRepository;
import lzzy.edu.cn.demothymeleaf.service.ArticleService;
import lzzy.edu.cn.demothymeleaf.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    @PostMapping("/comment/add/{id}")
    public String addComment(@PathVariable Integer id,@RequestParam String author,@RequestParam String content, Model model) {
        model.addAttribute("article", articleService.findById(id));
        commentService.addComment(author,content,id);
        return "redirect:/article/content/"+id;
    }

    @GetMapping("/article/content/{id}")
    public String articleContent(@PathVariable Integer id, Model model) {
        model.addAttribute("article", articleService.findById(id));
        model.addAttribute("commentList",commentService.selectByAId(id));
        return "content";
    }


}
